#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment'
require 'active_record'
data_dir= File.join(RAILS_ROOT, 'public', 'data')
puts "Populating information at #{data_dir}"
Dir.glob("#{data_dir}/*.txt").each do |f|
  file = File.open(f)
  attribute = File.basename(f).gsub('.txt','').to_sym
  file.each_line do |line|
    fields = line.split(',')
    value = fields[1].to_i
    name = fields[0].to_s
    pilot = Pilot.find_by_name(name)
    if pilot.nil?
      Pilot.create! :name => name, attribute => value
      puts "Created '#{name}' which attribute '#{attribute.to_s}' equals '#{value}'"
    else
      pilot.update_attributes attribute => value
      puts "Updated '#{name}' which attribute '#{attribute.to_s}' equals '#{value}'"
    end
  end
end
